/**
 * 
 */
package com.et114.modules.products.supplier.action;

import java.util.UUID;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.et114.core.common.CONST;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.utility.FileUploadUtil;
import com.et114.core.utility.ThreadSynchronizer;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.supplier.services.SupplierServiceImpl ; 
import com.et114.modules.products.supplier.vo.ProSuppliers;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;
import com.et114.components.simpletable.DataList;
/*
 * @author guanhw
 */
public class SupplierAction extends BaseDispatchAction {
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getSupplier( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		long ct = System.currentTimeMillis ( ) ; 
		//DIC
		List< BizOption > dicOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_PRO_TYPE_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		List< BizOption > csOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_COMMON_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		
		request.setAttribute ( "dicOps" , dicOps ) ;
		request.setAttribute ( "csOps" , csOps ) ;
		//BUSS
		SupplierServiceImpl ssbean = ( SupplierServiceImpl )getBean ( "supplierServiceImpl" ) ;
		
		SupplierForm supplierForm  = ( SupplierForm ) form ; 
		ProSuppliers psVo = new ProSuppliers( ) ; 
		PropertyUtils.copyProperties ( psVo , supplierForm ) ; 
		
		Long sid = supplierForm.getProSupplierId ( ) ;
		if ( sid != null && sid.longValue ( ) != 0  ) { //load 
			psVo = ssbean.getProSupplierById (  sid ) ; 
			request.setAttribute ( "supplier" , psVo ) ; 
			return mapping.findForward ( "supplier" ) ; 
		}
		else { //loads
			DataList dl =  ssbean.querySuppliers ( psVo ) ;  
			request.setAttribute(  "dl" , dl ) ;
			request.setAttribute ( "searchVo" , psVo ) ; 
			logger.info ( "loads supplers time:" + ( System.currentTimeMillis ( ) - ct ) ) ;
			return mapping.findForward ( "suppliers" ) ; 
		}
	}
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward toEditSupplier( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		List< BizOption > dicOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_PRO_TYPE_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		List< BizOption > csOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_COMMON_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "csOps" , csOps ) ;
		request.setAttribute ( "dicOps" , dicOps ) ;
		return mapping.findForward ( "supplier" ) ; 
	}
	
	
	public  ActionForward editSupplier( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC
	   
		//BUS
		SupplierServiceImpl ssbean = ( SupplierServiceImpl )getBean ( "supplierServiceImpl" ) ;
		SupplierForm supplierForm  = ( SupplierForm ) form ; 
		ProSuppliers psVo = new ProSuppliers( ) ; 
		PropertyUtils.copyProperties ( psVo , supplierForm ) ; 
		psVo.setCrtUser ( getCurrentUser ( request ).getUserId ( ) ) ;
		FileUploadUtil upload = new FileUploadUtil( ) ; 
		upload.setRestrictfs ( 102400 * 3 ) ;
		Long sid = supplierForm.getProSupplierId ( ) ;
		
		FormFile ffile = supplierForm.getProSupplierImgff ( ) ; 
		String prosif = ffile.getFileName ( ) ; 
		if ( sid != null && sid.longValue ( ) != 0  ) { //update 
			ProSuppliers o_psVo = ssbean.getProSupplierById ( sid ) ; 
			String phonep = o_psVo.getProSupplierImg ( ) ; 							
			if ( prosif != null && ! "".equals ( prosif.trim ( ) ) ) 
				FileUploadUtil.removeRealPathFile ( phonep , request ) ;
			else psVo.setProSupplierImg ( phonep ) ; 
			ssbean.updateSupplier ( psVo ) ;
		}
		else  //new
			sid = ssbean.saveSupplier ( psVo ) ; 
		
		
		if ( prosif != null && !"".equals ( prosif.trim ( )  ) ) {
			ThreadSynchronizer.threadEnter ( );
			try {
				String fn = upload.uploadv2 ( supplierForm.getProSupplierImgff ( ) , request , Const.PRO_SUPPLIER_IMG_UPLOAD_PATH , UUID.randomUUID ( ).toString ( ) );
				fn = Const.PRO_SUPPLIER_IMG_UPLOAD_PATH + fn;
				psVo.setProSupplierImg ( fn );
				ssbean.updateSupplier ( psVo );				
			} finally {
				ThreadSynchronizer.threadExit ( );
			}			
		}
		
		
		return new ActionForward ( "supplier.go?method=getSupplier&proSupplierId=" + sid ,
					true );

	}	
	
	
	public  ActionForward removeSupplier( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SupplierServiceImpl ssbean = ( SupplierServiceImpl )getBean ( "supplierServiceImpl" ) ;
		SupplierForm supplierForm  = ( SupplierForm ) form ; 
		Long[] deleteIds = supplierForm.getDeleteId ( ) ; 
		if ( deleteIds != null )
			for ( Long deleteId : deleteIds ) {
				//ProSuppliers vo = ssbean.getProSupplierById ( deleteId ); 
				//String imgp = vo.getProSupplierImg ( ) ; 
				//vo.setCrtUser ( getCurrentUser ( request ).getUserId ( ) ) ;
				//vo.setRemoveMark ( String.valueOf ( Const.COMMON_REMOVE_MARK_Y ) ) ; 
				//ssbean.updateSupplier ( vo ) ;
				
				ssbean.removeSupplier ( deleteId ) ;
				
				//FileUploadUtil.removeRealPathFile ( imgp , request ) ; 
			}
		return new ActionForward( "supplier.go?method=getSupplier" ,  true ) ;
	}
	
	
}
